#ifndef _CINEMATIC_EDITOR_H_
#define _CINEMATIC_EDITOR_H_

#include "CGlobals.h"
#include "CCinematicCommand.h"
#include "IEventReceiver.h"
#include "quaternion.h"
#include "IrrArray.h"


class CSceneObject;
class CProperty;
class CCinematicCommand;
class CCinematicThread;

namespace  irr
{
	namespace gui
	{
		class IGUIWindow;
		class IGUIComboBox;
		class IGUIEditBox;
		class IGUITab;
		class IGUIListBox;
		class IGUITabControl;
		class IGUICinematicCtrl;
		class IGUIStaticText;
		class IGUISpinBox;
	}
	namespace io
	{
		class IWriteFile;
	}
}
extern CGlobals* g;
//--------------------------------------------------------------------------------------------------
//
//--------------------------------------------------------------------------------------------------
struct SObjectParams
{
	vector3df pos;
	quaternion rot;
	vector3df scale;
};

#define EVENTS_INTERVAL		50
#define MAX_COLUMNS 1500

//--------------------------------------------------------------------------------------------------
//
//--------------------------------------------------------------------------------------------------
class CCinematicEditor
{
	public:
		CCinematicEditor();
		~CCinematicEditor();
		
		bool m_Active;

		void editCinematic(CSceneObject* object);
		void closeCinematic(bool bSave = false);
		CCinematicCommand* appendCommond(stringw cmdName, int column);
		bool receiveEvent(const SEvent& event);
		void updateWindowPosition();
		void setObjectID(s32 id);
		void setObjectProperties();
		void writeCinematicCommands(IWriteFile* f);
		void updateTimeLine();
		void UpdateCurrentCinematic();
		void enableKFButtons(bool );
		void SetThreadObject(s32 id);
		void AutoSetThreadObj(CCinematicThread* thread, int objType);
		void AutoSetPlayerThreadObj(CCinematicThread* thread);
		void MoveRangeCmds(bool doMove);

		void SetVisible( bool b );

		bool InitPreviewData();	

		void updateProperties();

		IGUICinematicCtrl* GetCinematicCtrl()
		{
			return m_cinematicCtrl;
		}

		int GetNowTime();

		bool propertyButtonPressed(const SEvent& event);

	private:
		IGUIWindow* m_CinematicWindow;		
		//IGUIWindow* m_CommandsWindow;		
		//IGUIWindow* m_CineNameInputWindow;
		IGUIListBox* m_KeyFrameList;
		IGUIComboBox* m_CommandBox;	
		//IGUIComboBox* m_ObjectsBox;
		//IGUIEditBox* m_CineNameInputEdit;
		IGUIEditBox* m_ObjectsBox;
		IGUIStaticText* m_ObjectsType;
		IGUITab* m_PropTab;

		IGUICinematicCtrl* m_cinematicCtrl;

		IGUISpinBox* m_moveRangeStartBox;
		IGUISpinBox* m_moveRangeEndBox;
		IGUISpinBox* m_moveRangeOffsetBox;

		CSceneObject* m_Target;
		stringc m_CineFile;
		int m_CurrentCinematicIndex;		
		array<CCinematicCommand*> m_CinematicCommands;
		array<CCinematicCommand*>* m_CurrentCinematic;
		//array<CCinematicCommand*>* m_ThreadAllCinematic
		//CCinematicCommand* m_CurrentCommand;
		CCinematicThread* m_CinematicThread;

		// for display selected cinematic Thread name.
		IGUIStaticText* m_cinematicThreadSelected;

		array<CProperty> m_CinematicProperties;
		array<stringw> animationList;
		array<SObjectParams> m_SavedObjectParameters;

		void loadCinematicCommands();
		void createCinematicWindow();
		
		void loadCinematic(stringc fileName);
		void saveCinematic(stringc fileName, bool close=true);
		void backupCinematic(stringc fileName, stringc backupExt);
		
		void clearCinematic();

		void createCommandProperties();
		int createProperty(stringw name, int type, s32 x, s32 y, IGUIElement* parent ,CCinematicCommand* );
		stringw FindStringBetween(stringw str, wchar_t c, int beginPos);
		
		bool propertyEditBoxEnter(const SEvent& event);
		void updateCurrentCinematicList();
		void executeCinematicToSelection();		
		s32 FindId( stringw name );
		CCinematicCommand* FindCinematic(stringw name);  

		void UpdateAnimationList(s32 objId);


		int	m_oldSelectedRow;
		int	m_oldSelectedCol;
		int	m_oldSelectedCount;
private:
		void UpdatePropOfChangeCamera(CCinematicCommand* cmd);
		void UpdatePropOfMoveObject(CCinematicCommand* cmd, int col);

		enum JUMP_KF_ACTION
		{
			KF_FIRST,
			KF_LAST,
			KF_PREV,
			KF_NEXT
		};

		void JumpToKeyFrame(JUMP_KF_ACTION jumpAction);

public:	
		array<CCinematicThread*> m_objThreads;
		CCinematicThread* m_cameraThread;
	
};
//--------------------------------------------------------------------------------------------------
//
//--------------------------------------------------------------------------------------------------
#endif